var app = getApp()
Page({
  data: {
    
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.title){
      that.setData({
        title:e.title
      })
    }
    if (that.data.title) {
      app.setNavtitle(that.data.title);
    } else {
      app.setNavtitle("收藏有礼");
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestSetting();
    }
  },
  onShow: function () {

  },
  onPullDownRefresh: function(){
    var that = this;
    that.requestSetting();
  },
  requestSetting:function(){
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_collection_prize_setting'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var dataInfo = res.data.data;
          that.setData({
            settingInfo: dataInfo,
            status: dataInfo.status
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  // 上传图片
  chooseSingleImage: function (e) {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  //选择单图
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        var temPath = res.tempFilePaths[0];
        that.upload_file(temPath);
      }
    })
  },
  // 上传单图到服务器
  upload_file: function (temPath) {
    var that = this;
    var formData = that.data.formData;
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: app.globalData.requestUrl + '?&map=applet_img_upload',
      filePath: temPath,
      name: 'image',
      success: function (res) {
        var data = JSON.parse(res.data);
        var realpath = app.globalData.domin + data.data.path;
        if (data.ec == 200) {
          that.setData({
            showimgTemp: data.data.path,
            showimg: realpath
          })
          console.log(that.data.showimgTemp);
          console.log(that.data.showimg);
        } else {
          wx.showModal({
            title: '提示',
            content: data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  backbtn:function(){
    wx.navigateBack({
      delta:1
    })
  },
  toseeGift: function () {
    wx.navigateTo({
      url: '/subpages/myCoupon/myCoupon'
    })
  },
  newSubmit: function () {
    var that = this;
    that.setData({
      status:0
    })
  },
  submitAudit:function(){
    var that = this;
    var data = {
      map: 'applet_collection_prize_submit',
      image: that.data.showimgTemp ? that.data.showimgTemp:''
    }
    if (!data.image){
      app.errorTip(that, "请上传截图", 2000);
      return;
    }
    console.log(data);
    wx.showModal({
      title: '确认提示',
      content: '确定提交审核吗？',
      success(res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '提交中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              console.log(res.data);
              if (res.data.ec == 200) {
                that.requestSetting();
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})